//
//  JXModifyInfoController.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/8.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXModifyInfoController.h"
#import "ModifyHeadImgBtn.h"
#import "ZJSwitch.h"
#import "JTSActionSheet.h"
#import "SelfDataTool.h"

#import <MobileCoreServices/MobileCoreServices.h>

#define ITEM_HEIGHT [DimensUtils getScaleX:45]

@interface JXModifyInfoController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
{
    ModifyHeadImgBtn *_headImgBtn;
    UITextField *_nameField;
    
    UIView *_genderView;
    ZJSwitch *_genderSwitch;
    
    UIImagePickerController *_imagePickerController;
    
    BOOL ISCHANGEPIC;//是否更换了头像图片
}

@end

@implementation JXModifyInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    ISCHANGEPIC = NO;
    self.navigationItem.title = @"修改";
    [self.view setBackgroundColor:JXGlobalColorBg];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    [self initViews];
}

-(void)initViews{
    _headImgBtn = [[ModifyHeadImgBtn alloc]initWithHeadImgUrl:[UserDefaultsHelper stringForKey:KEY_USER_IMG]];
    _headImgBtn.x = 0;_headImgBtn.y = NavHeight;
    [_headImgBtn addTarget:self action:@selector(choseImgBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_headImgBtn];
    
    UIView *splite1  = [[UIView alloc]init];
    splite1.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(_headImgBtn.frame), JXScreenW-JXPublicInset, 0.5);
    [splite1 setBackgroundColor:JXColor(204, 204, 204)];
    [self.view addSubview:splite1];
    
    
    _nameField = [[UITextField alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(splite1.frame), JXScreenW, ITEM_HEIGHT)];
    [_nameField setTextColor:JXColor(102, 102, 102)];
    [_nameField setPlaceholder:@"用户名"];
    [_nameField setFont:[UIFont systemFontOfSize:NAME_FONT]];
    [_nameField setText:[UserDefaultsHelper stringForKey:KEY_NICK_NAME]];
    [self.view addSubview:_nameField];
    
    UIView *splite2  = [[UIView alloc]init];
    splite2.frame = CGRectMake(JXPublicInset, CGRectGetMaxY(_nameField.frame), JXScreenW-JXPublicInset, 0.5);
    [splite2 setBackgroundColor:JXColor(204, 204, 204)];
    [self.view addSubview:splite2];
    
    
    _genderView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(splite2.frame), JXScreenW, ITEM_HEIGHT)];
    [self.view addSubview:_genderView];
    
    UILabel *sexLabel = [[UILabel alloc]init];
    [sexLabel setFont:[UIFont systemFontOfSize:NAME_FONT]];
    [sexLabel setText:@"性别"];
    [sexLabel setTextColor:JXColor(102, 102, 102)];
    CGSize size = [sexLabel.text sizeWithFont:sexLabel.font];
    [sexLabel setFrame:CGRectMake(JXPublicInset, (_genderView.height-size.height)/2, size.width, size.height)];
    [_genderView addSubview:sexLabel];
    
    _genderSwitch = [[ZJSwitch alloc] initWithFrame:CGRectMake(JXScreenW-JXPublicInset-60, (_genderView.height-25)/2, 60, 25)];
    _genderSwitch.onTintColor = JXThemeColor;
    _genderSwitch.tintColor = JXColor(51, 195, 248);
    _genderSwitch.onText = @"女";
    _genderSwitch.offText = @"男";
    [_genderView addSubview:_genderSwitch];
    
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUp:)];
    [self.view addGestureRecognizer:singleTap1];
    
}

-(void)touchUp:(UIGestureRecognizer *)ges{
    [self.view endEditing:YES];
}

-(void)choseImgBtnClick{
    [self.view endEditing:YES];
    
    JTSActionSheetTheme *theme = [JTSActionSheetTheme defaultTheme];
    theme.separatorColor = JXColor(204, 204, 204);
    theme.normalButtonColor = JXThemeColor;
    theme.destructiveButtonColor = [UIColor blackColor];
    theme.titleColor = JXThemeColor;
    theme.cancelButtonColor = [UIColor blackColor];
    
    JTSActionSheetItem *buyAll = [JTSActionSheetItem itemWithTitle:@"拍照" action:^{
        //前往拍照
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePickerController.delegate = self;
        _imagePickerController.allowsEditing = YES;
        //设置拍照时的下方的工具栏是否显示，如果需要自定义拍摄界面，则可把该工具栏隐藏
        _imagePickerController.showsCameraControls  = YES;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    } isDestructive:NO];
    
    JTSActionSheetItem *deleteAll = [JTSActionSheetItem itemWithTitle:@"从手机相册选择" action:^{
        //前往手机相册
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _imagePickerController.delegate = self;
        _imagePickerController.allowsEditing = YES;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    } isDestructive:NO];
    
    JTSActionSheetItem *cancel = [JTSActionSheetItem itemWithTitle:@"取消" action:^{
    } isDestructive:NO];
    
    JTSActionSheet *sheet = [[JTSActionSheet alloc] initWithTheme:theme
                                                            title:nil
                                                      actionItems:@[buyAll, deleteAll]
                                                       cancelItem:cancel];
    [sheet showInView:self.view];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    ISCHANGEPIC = YES;
    //获取图片裁剪的图
    UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
    [_headImgBtn.headImage setImage:edit];
    
    //模态方式退出uiimagepickercontroller
    [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  获取用户信息
 */
-(void) getUserInfo:(MBProgressHUD *)HUD{
    [SelfDataTool getUserInfoWithUserId:@"" success:^(UserInfo *result){
        [HUD hide:YES];
        if([Utils isBlankString:[result data]]){//获取用户信息成功
            //1.保存用户信息
            [Utils saveUserInfo:result];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {//获取用户信息失败
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"重置本地个人信息失败"];
    }];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //用户点击了取消
    [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

-(void)save{
    if ([Utils getLengthWithStr:_nameField.text]>16) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"用户名不能超过8个汉字"];
        return;
    }
    
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.navigationController.view];
    [SelfDataTool setUserInfo:_nameField.text userGender:!_genderSwitch.on?@"1":@"2" userImage:ISCHANGEPIC?_headImgBtn.headImage.image:nil success:^(JXBaseResponse *result) {
        if ([Utils isBlankString:result.data]) {
            //在这个地方去执行获取个人信息
            [self getUserInfo:HUD];
            [DialogUtils showTipsWithView:self.navigationController.view String:@"保存成功"];
            ISCHANGEPIC = NO;
        }else{
            [HUD hide:YES];
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
            
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"保存个人信息失败s"];
    }];
}



@end
